/*
 * Copyright 2017 the original author or authors.
 *
 */

package com.ivo.util;

import java.io.UnsupportedEncodingException;
import java.util.UUID;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.lang3.StringEscapeUtils;

/**
 * <h1>编码工具类</h1>
 * <p>
 * content
 * </p>
 * <b>Note:</b>
 *
 * @author nexf
 * @since 2015/10/2 13:46
 */
public class EncodeUtil {

  private static final String DEFAULT_STRING_ENCODING = "UTF-8";
  private static final String DEFAULT_URL_ENCODING = "UTF-8";
  private static final char[] BASE62 =
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();

  /**
   * Hex编码.
   */
  public static String encodeHex(byte[] input) {
    return Hex.encodeHexString(input);
  }

  /**
   * Hex解码.
   */
  public static byte[] decodeHex(String input) {
    try {
      return Hex.decodeHex(input.toCharArray());
    } catch (DecoderException e) {
      throw ExceptionUtil.unchecked(e);
    }
  }

  /**
   * Base64编码.
   */
  public static String encodeBase64(String str) {
    if (str == null) {
      return null;
    }
    try {
      return encodeBase64(str.getBytes(DEFAULT_STRING_ENCODING));
    } catch (UnsupportedEncodingException e) {
      return null;
    }
  }


  public static String encodeBase64(byte[] input) {
    return Base64.encodeBase64String(input);
  }

  /**
   * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548).
   */
  public static String encodeUrlSafeBase64(String str) {
    if (str == null) {
      return null;
    }
    try {
      return encodeUrlSafeBase64(str.getBytes(DEFAULT_URL_ENCODING));
    } catch (UnsupportedEncodingException e) {
      return null;
    }
  }

  public static String encodeUrlSafeBase64(byte[] input) {
    return Base64.encodeBase64URLSafeString(input);
  }

  /**
   * Base64解码.
   */

  public static String decodeBase64String(String str) {
    if (str == null) {
      return null;
    }
    try {
      byte[] b = decodeBase64(str);
      return new String(b, DEFAULT_STRING_ENCODING);
    } catch (UnsupportedEncodingException e) {
      return null;
    }
  }

  public static byte[] decodeBase64(String input) {
    return Base64.decodeBase64(input);
  }

  /**
   * Base62编码。
   */
  public static String encodeBase62(byte[] input) {
    char[] chars = new char[input.length];
    for (int i = 0; i < input.length; i++) {
      chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)];
    }
    return new String(chars);
  }

  /**
   * Html 转码.
   */
  public static String escapeHtml(String html) {
    return StringEscapeUtils.escapeHtml4(html);
  }

  /**
   * Html 解码.
   */
  public static String unEscapeHtml(String htmlEscaped) {
    return StringEscapeUtils.unescapeHtml4(htmlEscaped);
  }

  /**
   * Xml 转码.
   */
  public static String escapeXml(String xml) {
    return StringEscapeUtils.escapeXml10(xml);
  }

  /**
   * Xml 解码.
   */
  public static String unescapeXml(String xmlEscaped) {
    return StringEscapeUtils.unescapeXml(xmlEscaped);
  }

  /**
   * URL 编码, Encode默认为UTF-8.
   */
  public static String urlEncode(String part) {
    try {
      return new URLCodec(DEFAULT_URL_ENCODING).encode(part);
    } catch (EncoderException e) {
      throw ExceptionUtil.unchecked(e);
    }
  }

  /**
   * URL 解码, Encode默认为UTF-8.
   */
  public static String urlDecode(String part) {
    try {
      return new URLCodec(DEFAULT_URL_ENCODING).decode(part);
    } catch (DecoderException e) {
      throw ExceptionUtil.unchecked(e);
    }
  }

  /**
   * 生成一个6位数的验证码。
   *
   * @return 验证码
   */
  public static String generateCheckCode() {
    String time = String.valueOf(System.nanoTime());
    while (time.length() > 6) {
      time = time.substring(1, time.length());
    }

    return time;
  }


  /**
   * 生成UUID
   *
   * @return UUID
   */
  public static String createUUID() {
    return UUID.randomUUID().toString();
  }

  /**
   * 生成GUID
   *
   * @return GUID
   */
  public static String createGUID() {
    return createUUID().replace("-", "");
  }


  public static void main(String[] args) {
    System.out.println(createGUID());
  }
}
